package com.ruoyi.workflow.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.system.api.RemoteWorkflowUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.WfUserDTO;
import com.ruoyi.system.api.model.WfUserMultiDTO;
import com.ruoyi.workflow.domain.vo.MultiVo;
import com.ruoyi.workflow.service.IUserService;
import com.ruoyi.workflow.utils.WorkFlowUtils;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * @description: 选人业务层
 * @author: gssong
 * @date: 2021/10/17 14:57
 */
@RequiredArgsConstructor
@Service
public class UserServiceImpl implements IUserService {

    @DubboReference
    private RemoteWorkflowUserService remoteWorkflowUserService;
    private final TaskService taskService;
    private final RuntimeService runtimeService;

    /**
     * @description: 按照用户id查询用户集合
     * @param: userIds
     * @return: java.util.List<com.ruoyi.common.core.domain.entity.SysUser>
     * @author: gssong
     * @date: 2021/12/10
     */
    @Override
    public List<SysUser> selectListUserByIds(List<Long> userIds) {
        return remoteWorkflowUserService.selectUserListByIds(userIds);
    }

    /**
     * @description: 按照用户id查询用户
     * @param: userId
     * @return: com.ruoyi.common.core.domain.entity.SysUser
     * @author: gssong
     * @date: 2021/12/10
     */
    @Override
    public SysUser selectUserById(Long userId) {
        return remoteWorkflowUserService.selectUserById(userId);
    }

    /**
     * @description: 分页查询工作流选人, 角色，部门等
     * @param: sysUserBo
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @author: gssong
     * @date: 2021/12/10
     */
    @Override
    public Map<String, Object> getWorkflowUserListByPage(WfUserDTO wfUserDTO) {
        return remoteWorkflowUserService.getWorkflowUserListByPage(wfUserDTO);
    }

    /**
     * @description: 分页查询工作流选择加签人员
     * @param: sysUserMultiBo
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @author: gssong
     * @date: 2022/4/22 21:17
     */
    @Override
    public Map<String, Object> getWorkflowAddMultiListByPage(WfUserMultiDTO wfUserMultiDTO) {
        Task task = taskService.createTaskQuery().taskId(wfUserMultiDTO.getTaskId()).singleResult();
        MultiVo multiInstance = WorkFlowUtils.isMultiInstance(task.getProcessDefinitionId(), task.getTaskDefinitionKey());
        if (ObjectUtil.isNotEmpty(multiInstance)) {
            wfUserMultiDTO.setMulti(true);
            List<Long> assigneeList = (List) runtimeService.getVariable(task.getExecutionId(), multiInstance.getAssigneeList());
            wfUserMultiDTO.setMultiAssigneeList(assigneeList);
        }
        return remoteWorkflowUserService.getWorkflowAddMultiListByPage(wfUserMultiDTO);
    }

    /**
     * @description: 查询审批人
     * @param: params
     * @param: chooseWay
     * @param: nodeName
     * @return: java.util.List<java.lang.Long>
     * @author: gssong
     * @date: 2022/12/11 19:17
     */
    @Override
    public List<Long> getAssigneeIdList(String params, String chooseWay, String nodeName) {
        return remoteWorkflowUserService.getAssigneeIdList(params,chooseWay,nodeName);
    }
}
